package cn.school.educational.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.school.common.api.CommonResult;
import cn.school.common.exception.ApiException;
import cn.school.common.utils.SysUserUtils;
import cn.school.educational.dao.SubjectDao;
import cn.school.educational.entity.Subject;
import cn.school.educational.service.SubjectService;
import cn.school.educational.vo.SubjectExcelVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

    
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

@Service
public class SubjectServiceImpl extends ServiceImpl<SubjectDao, Subject> implements SubjectService {
    @Resource
    SubjectDao subjectDao;

    @Override
    public List<Subject> listByClassId(Map<String, Object> para) {
        return subjectDao.listByClassId(para);
    }

    @Override
    public CommonResult<String> importSubjectExcel(InputStream inputStream) throws IOException {
        XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
        XSSFSheet sheetAt = workbook.getSheetAt(0);
        List<SubjectExcelVo> list = new ArrayList<>();
        SubjectExcelVo subExcel = null;
        for (int i=1;i<sheetAt.getPhysicalNumberOfRows();i++){
            Row row = sheetAt.getRow(i);
            subExcel = new SubjectExcelVo();
            if (i > 1) {
                for (int j = 0;j<row.getLastCellNum();j++){
                    Cell cell = row.getCell(j);
                    if (j == 0) {
                        subExcel.setSubjCode(cell.getStringCellValue());
                    }
                    if (j == 1) {
                        subExcel.setSubjName(cell.getStringCellValue());
                    }
                }
                list.add(subExcel);
            }
        }
        int row = insertSub(list);
        if (row > 0) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @Override
    public Subject getSubjectById(long id) {
        return this.baseMapper.selectById(id);
    }

    private int insertSub(List<SubjectExcelVo> vo){
        int num=0;
        Subject subject = null;
        for (int j=0;j<vo.size();j++){
            LambdaQueryWrapper<Subject> subjectQueryWrapper = new LambdaQueryWrapper<>();
            if (StrUtil.isNotEmpty(vo.get(j).getSubjName())) {
                subjectQueryWrapper.eq(Subject::getSubjName, vo.get(j).getSubjName());
                int count = this.baseMapper.selectCount(subjectQueryWrapper);
                if (count > 0) {
                    throw new ApiException("科目名称出现重复，请检查科目名称");
                }
            }
        }
        for(int i=0;i<vo.size();i++) {
            subject = new Subject();
            subject.setSubjName(vo.get(i).getSubjName());
            subject.setSubjCode(vo.get(i).getSubjCode());
            subject.setCreateTime(Calendar.getInstance().getTime());
            subject.setModifyTime(Calendar.getInstance().getTime());
            subject.setCreateBy(SysUserUtils.getSysUser().getId());
            int row = baseMapper.insert(subject);
            if (row > 0) {
                num+=1;
            }
        }
        if (num < vo.size()) {
            return num = 0;
        }
        return num;
    }
}
